﻿#pragma once
#include <memory>
#include "IShape.h"
#include "BasicShape.h"
#include "line.h"

class ShapeFactory : public QObject {
    Q_OBJECT
public:
    // 获取单例（C++11线程安全）
    static ShapeFactory& instance(QObject* parent = nullptr) {
        static ShapeFactory instance(parent);
        return instance;
    }

     //注册图形（示例：注册矩形）
    template<typename ShapeType>
    void registerShape(const QString& name) {
        m_shapes.emplace(name, std::make_unique<ShapeType>());
    }

    //创建图像
    std::unique_ptr<IShape> create(const QString& name, const QPointF& pos = { 0, 0 }) const {
        auto it = m_shapes.find(name);
        if (it != m_shapes.end()) {
            auto shape = it->second->init(pos);
            return shape;
        }
        return nullptr;
    }

    //// 获取所有图形名称
    QVector<QString> availableShapes() const {
        QVector<QString> keys;
        for (const auto& pair : m_shapes) {
            keys.push_back(pair.first);
        }
        return keys;
    }

    
    // 禁止拷贝
    ShapeFactory(const ShapeFactory&) = delete;
    ShapeFactory& operator=(const ShapeFactory&) = delete;

private:
    explicit ShapeFactory(QObject* parent = nullptr) : QObject(parent) {
        // 默认注册流程图常用图形
        registerShape<Line>("Line");
        registerShape<Rectangle>("Rectangle");
        registerShape<Ellipse>("Ellipse");
        registerShape<Diamond>("Diamond");
        registerShape<Parallelogram>("Parallelogram");

        registerShape<Triangle>("Triangle");
        registerShape<RoundedRectangle>("RoundedRectangle");
        registerShape<Pentagon>("Pentagon");
        registerShape<Hexagon>("Hexagon");

    
    }  // 初始化QObject父类

    std::map<QString, std::unique_ptr<IShape>> m_shapes;
};